import cv2 as cv
import numpy as np

def showImg(x):
    ret, threshImg = cv.threshold(img, x, 255, cv.THRESH_TOZERO)
    cv.imshow('imgWin', threshImg)
    #newImg = cv.morphologyEx(threshImg, cv.MORPH_GRADIENT, kernel, iterations=1) #梯度
    #newImg = cv.morphologyEx(threshImg, cv.MORPH_TOPHAT, kernel, iterations=2)   #顶帽
    newImg = cv.morphologyEx(threshImg, cv.MORPH_BLACKHAT, kernel, iterations=2) #黑帽
    cv.imshow('newImgWin', newImg)

img = cv.imread("assets/book.jpg", 0)
cv.imshow('imgWin', img)

kernel =np.ones((3,3),np.uint8)

# 创建滑块,注册拖动时的回调函数 showImg
cv.createTrackbar('slider', 'imgWin', 100, 255, showImg)


cv.waitKey(0)
